<template>
  <div class="login-page">
      <div class="login-container">
        <div class="login-header">
          <el-icon class="logo-icon"><Cpu /></el-icon>
          <h1 class="title">PCB芯选</h1>
          <p class="subtitle">用户登录</p>
        </div>
        
        <el-form
          ref="loginForm"
          :model="loginData"
          :rules="rules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginData.username"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginData.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="login-btn"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p>还没有账号？ <router-link to="/register" class="register-link">立即注册</router-link></p>
          <p><router-link to="/admin/login" class="admin-link">管理员登录</router-link></p>
          <p><router-link to="/" class="back-home">返回首页</router-link></p>
        </div>
      </div>
    </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { userAPI } from '@/api'
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginView',
  setup() {
    const router = useRouter()
    const store = useStore()
    const loading = ref(false)
    const loginForm = ref()
    
    const loginData = reactive({
      username: '',
      password: ''
    })
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      try {
        await loginForm.value.validate()
        loading.value = true
        
        const response = await userAPI.login(loginData)
        
        if (response.success) {
          // 保存用户信息到store
          store.commit('setUser', response.user)
          
          ElMessage.success('登录成功')
          router.push('/')
        } else {
          ElMessage.error(response.message || '登录失败')
        }
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error('登录失败，请检查网络连接')
      } finally {
        loading.value = false
      }
    }
    
    return {
      loginData,
      rules,
      loading,
      loginForm,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-container {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-icon {
  font-size: 50px;
  color: #00D4FF;
  margin-bottom: 20px;
}

.title {
  font-size: 2rem;
  margin-bottom: 10px;
  background: linear-gradient(45deg, #00D4FF, #4F46E5);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: bold;
}

.subtitle {
  color: #B0BEC5;
  font-size: 1.1rem;
}

.login-form {
  margin-bottom: 30px;
}

.login-form :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: none;
}

.login-form :deep(.el-input__wrapper):hover {
  border-color: #00D4FF;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #00D4FF;
  box-shadow: 0 0 0 1px rgba(0, 212, 255, 0.3);
}

.login-form :deep(.el-input__inner) {
  color: #ffffff;
}

.login-form :deep(.el-input__inner::placeholder) {
  color: #B0BEC5;
}

.login-form :deep(.el-icon) {
  color: #B0BEC5;
}

.login-btn {
  width: 100%;
  background: linear-gradient(45deg, #00D4FF, #4F46E5);
  border: none;
  font-size: 1.1rem;
  font-weight: bold;
}

.login-btn:hover {
  background: linear-gradient(45deg, #00B8E6, #4338CA);
}

.login-footer {
  text-align: center;
  color: #B0BEC5;
}

.login-footer p {
  margin: 10px 0;
}

.register-link,
.admin-link,
.back-home {
  color: #00D4FF;
  text-decoration: none;
  transition: color 0.3s ease;
}

.register-link:hover,
.admin-link:hover,
.back-home:hover {
  color: #00B8E6;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 1.5rem;
  }
}
</style> 